package com.common.app.utls

import android.Manifest
import android.app.Activity
import android.app.AppOpsManager
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import androidx.core.app.ActivityCompat
import com.common.base.app.extras.getContext
import com.common.base.utils.AppUtils
import java.lang.reflect.InvocationTargetException

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/29
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
object PermissionUtils {
    /**
     * 访问相机、文件的权限
     */
    val REQ_PERMISSION_CODE = 0x100

    fun checkPermission(activity: Activity): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity != null) {
            val permissions = java.util.ArrayList<String>()
            if (PackageManager.PERMISSION_GRANTED != activity.let {
                    ActivityCompat.checkSelfPermission(
                        it,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    )
                } ||
                PackageManager.PERMISSION_GRANTED != activity.let {
                    ActivityCompat.checkSelfPermission(
                        it,
                        Manifest.permission.CAMERA
                    )
                }) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                permissions.add(Manifest.permission.CAMERA)
            }
            if (permissions.size != 0) {
                ActivityCompat.requestPermissions(
                    activity,
                    permissions.toTypedArray(),
                    REQ_PERMISSION_CODE
                )
                return false
            }
        }

        return true
    }

    /**
     * 跳转到权限设置界面
     */
    private fun getAppDetailSettingIntent(activity: Activity) {
        val intent = Intent()
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
        intent.data = Uri.fromParts("package", AppUtils.getPackageName(), null)
        activity.startActivity(intent)
    }

    fun checkNoticeEnabled(): Boolean {
        return if (Build.VERSION.SDK_INT < 24) {
            if (Build.VERSION.SDK_INT >= 19) {
                val appOps = getContext().getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
                val appInfo = getContext().applicationInfo
                val pkg = getContext().applicationContext.packageName
                val uid = appInfo.uid
                try {
                    val appOpsClass = Class.forName(AppOpsManager::class.java.name)
                    val checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String::class.java)
                    val opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION")
                    val value = opPostNotificationValue.get(Integer::class.java) as Int
                    checkOpNoThrowMethod.invoke(appOps, value, uid, pkg) as Int == 0
                } catch (e: NoSuchMethodException) {
                    true
                } catch (e: NoSuchFieldException) {
                    true
                } catch (e: InvocationTargetException) {
                    true
                } catch (e: IllegalAccessException) {
                    true
                } catch (e: RuntimeException) {
                    true
                } catch (e: ClassNotFoundException) {
                    true
                }
            } else {
                true
            }
        } else {
            val mNotificationManager = getContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            mNotificationManager != null && mNotificationManager.areNotificationsEnabled()
        }
    }
}